<script setup>
import { ref,getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { buyEgg } from '../../http/api'
import prizePopup from "../prizePopup/index.vue";
const { proxy } = getCurrentInstance()
let numberExchange = ref(proxy.$numberExchange)
// 用户信息对象
let userInfo = ref(undefined)

// 判断用户是否登录
let userInfos = localStorage.getItem('userInfo')
if (userInfos) {
    userInfo.value = JSON.parse(userInfos)
    console.log(userInfo, 'userInfos')
}
const router = useRouter()
const baseUrl = ref(import.meta.env.VITE_APP_BASE_URL)
// 扭蛋分类数组
// let eggSortInfoList = reactive([{},{},{},{},{},{},{},{},{},])
const emit = defineEmits(['emitId','toPageEmit'])
const props = defineProps({
    title: {
        type: String
    },
    isShowRight: {
        type: Boolean,
        default: true
    },
    egglist: {
        type: Array
    },
    isGoPage:{ //是否跳转页面
        type:Boolean,
        default:true
    },
    detailInfo:{
        type:Object,
        default:{}
    },
    BoxId:{ //获取扭蛋id(用于获取盲盒详情跳转抽奖页面)
        type:String,
        default:''
    },
    pageType:{ //页面类型（prize 奖品清单页面）
        type:String,
        default:''
    },
    viewDetailText:{
        type:String,
        default:'查看详情'
    }
})
console.log(props.egglist, 'props egglist')

// 跳转页面方法
const toPage = (id) => {
    console.log(props.BoxId,'boxid')
    
    // return 
    if(props.isGoPage){
        // return
        if(props.pageType === 'prize'){
            emit('emitId',id)
            
        }else{
            router.push({name:`raffle`,query:{id}})

        }

    }else{
        emit('emitId',id)
        maskShow2.value = true
    }
}

// 扭蛋弹窗是否显示
let maskShow2 = ref(false)
// 子组件返回的关闭弹窗事件回调
const maskShow2Change = (val)=>{
    maskShow2.value = val
}
// 购买点击
const buyEggClcik = (id) => {
    if(!userInfo.value){
        return ElMessage.warning(`${$t('msg.message.PleaseLog')}！`)
    }
    // choujiangShow.value = true

    setTimeout(async () => {

        let res = await buyEgg({ boxId: id })
        console.log(res, '扭蛋扭的结果')
        if (res.success) {
            ElMessage({
                message: $t('msg.message.buySuccess'),
                type: 'success',
            })
            // winPrize.data = res.result
            // choujiangShow.value = false

            maskShow2.value = false
        } else {
            let msg = ''
            if(res.message === "user wallet balance is not enough"){
                msg = $t('msg.message.goToRecharge')+'！'
                setTimeout(()=>{
                    router.push({name:'personCenter',query:{pageIndex:'7'}})

                },2000)
            }else{
                msg = res.message
            }
            
            ElMessage.error(msg)


        }

    }, 2000)
    // return

    // if(res){
    //    
    // }else{
    //    
    // }
}
const buyEmit = (id)=>{
    buyEggClcik(id)
}
const toPageEmit = (id)=>{
    emit('toPageEmit',id)
}
</script>

<template>
    <div class="eggSortInfo">
        <div class="eggSortInfoitem" v-for="(item, index) in egglist" :key="index">
            <div class="eggSortInfoitemImg">
                <img :src="item.img.substr(0, 1) === '/' ? baseUrl +'sys/common/static/'+ item.img : baseUrl + '/sys/common/static/' + item.img" alt="">
            </div>
            <div class="eggSortInfoitemMsg">
                <div class="eggSortInfoitemMsgL">
                    <div class="eggSortInfoitemMsgLText">
                        {{ item.name }}
                    </div>
                    <div class="eggSortInfoitemMsgLBottom">
                        <img src="../../assets/img/header/money.png" alt="">
                        <div>{{ numberExchange(item.val) }}</div>
                    </div>
                </div>
                <!-- <div class="findMoreBtn" >发现更多</div> -->
            </div>
            <img src="../../assets/img/raffle/blackBg.png" class="blackBg" alt="">
            <div class="lookDetail"  @click="toPage(item.id)">
                <img src="../../assets/img/home/yellowbtn.png" alt="">
                <div>{{$t("msg.message.viewDetails")}}</div>
            </div>
            <!-- <div class="eggSortInfoitemMsg">
                <div class="eggSortInfoitemMsgL">
                    <div class="eggSortInfoitemMsgLBig">
                        <div class="eggSortInfoitemMsgLText">
                            {{ item.name || '' }}
                        </div>
                        <div class="eggSortInfoitemMsgLBottom">
                            <img src="../../assets/img/header/money.png" alt="">
                            <div>{{numberExchange( item.price) || numberExchange(item.val) || '' }}</div>
                        </div>
                    </div>
                    <div :class="viewDetailText === '查看详情'  ? 'lookDetail' : 'lookDetail w58'">
                        {{viewDetailText}}
                    </div>
                </div>
            </div> -->
        </div>
    </div>
   <prizePopup :detailInfo="detailInfo" @toPageEmit="toPageEmit" @maskShow2Change="maskShow2Change" @buyEmit="buyEmit" :maskShow2="maskShow2"></prizePopup>
</template>

<style scoped>
.eggSortInfo {
    display: flex;
    /* margin: 0 240px; */
    /* margin-top: 24px; */
    /* justify-content: space-between; */
    flex-wrap: wrap;
    /* justify-content: center; */
    max-width: 1818px;
    margin: 0px 210px;
    min-width: 1490px;
    margin-top: 24px;

}

.eggSortInfoitem {
    display: flex;
    flex-direction: column;
    margin-right: 20px;
    padding: 12px;
    /* background: url('../../assets/img/home/eggBg.png'); */
    margin-bottom: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100% 100%;
    width: 319px;
    height: 402px;
    background: #282B29;
    /* margin-right: 33px; */
    position: relative;
    align-items: center;
}

.eggSortInfoitemImg {
    width: calc(100% - 10px);
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 11px;
    /* border-radius: 6px; */
    /* padding-top: 50px; */
    height: 285px;
}
.blackBg {
    position: absolute;
    width: 320px;
    
    top: 320px;
    left: -6px;
    height: 86px;
}
.eggSortInfoitemImg img {
    height: 47%;
}
.w58{
    width: 58px !important;
}
.eggSortInfoitemMsg {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* padding-top: 20px; */
    width: 100%;
    position: relative;
}

.eggSortInfoitemMsgL {
    display: flex;
    flex-direction: column;
    padding-left: 9px;
    position: absolute;
    top: 20px;
    z-index: 10;
    width: 70%;
}

.eggSortInfoitemMsgLText {
    font-size: 16px;
    color: #fff;
    height: 42px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    width: 100%;
}

.eggSortInfoitemMsgLBottom {
    display: flex;
    align-items: center;
}

.eggSortInfoitemMsgLBottom img {
    width: 16px;
    margin-right: 4px;
}

.eggSortInfoitemMsgLBottom div {
    font-size: 16px;
    color: #F6AE41;
}


.findMoreBtn {
    font-size: 20px;
    color: #F6AE41;
    border: 1px solid;
    display: flex;
    height: 38px;
    align-items: center;
    padding: 4px 15px;
    border-radius: 36px;
    cursor: pointer;
}
.eggSortInfoitemMsgLBig{
    display: flex;
    flex-direction: column;
    width: 70%;
}
.lookDetail {
    width: 103px;
    height: 50px;
    top: 30px;
    left: 123px;
    font-size: 16px;
    position: relative;
    font-weight: bold;
    cursor: pointer;
}
.lookDetail img{
   position: absolute;
   width: 103px;
   height: 50px;
    top: 0;
    left: 0;
}
.lookDetail div{
    position: absolute;
    width: 103px;
    height: 50px;
     /* height: 100%; */
     line-height: 50px;
     top: 0;
     text-align: center;
     left: 0;
     
     z-index: 5;
 }
 @media screen and (max-width:1600px) {
    .eggSortInfo{
        width: 1350px;
        min-width: unset;
        justify-content: center;
    }
}
</style>
